//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-5-12
// @Time:上午11:34
//

#ifndef E2EEMESSAGE_GROUPSESSION_H
#define E2EEMESSAGE_GROUPSESSION_H

#include "Common.h"
#include "CommonType.h"
#include <vector>
/**
 * @brief  群组 的设计是 , 进群的时候 互相交换 kdf
 *
 * 然后 同步
 *
 */

typedef void (* lockFunction)(void *userdata);
struct MemberCipherData{
    signal_protocol_sender_key_name *session ;
    group_cipher* cipher;

    virtual ~MemberCipherData();
};

struct MessagePackageDagaType{
    int type ;
    std::string serialze;
};

class GroupSession {
private:
    group_cipher *current_group_cipher = 0;
    // 储存群成员的 cipher
    std::map<std::string ,group_cipher* > memberCipher;

    std::string teamname;

    std::string myName ;
    int myDeviceid ;
    signal_context *m_global_context = 0 ;
public:
    signal_context *getMGlobalContext() const;

private:

    group_cipher *my_cipher = 0 ;

    signal_protocol_sender_key_name *my_sender_key_name  = 0 ;

    signal_protocol_store_context *my_store = 0;

    group_session_builder *my_session_builder = 0;

    std::vector<char*> verterChar;

    ec_key_pair *ik = 0 ;

    int local_id ;
public:

    GroupSession();
    /**
     * @brief 生成我的分发包
     * @param outDistributionPackage
     */
    void createDistribution(std::string &outDistributionPackage);

    // 收到分发包的时候 重新建立 group_cipher
    void updataDistribution(std::string sendername , int deviceid ,std::string distribution_message);

    // 成员推出的时候 移除 memberCipher
    void removeTeamMember(std::string teamname);

    // 发送消息的时候 , 通过 每个群成员 分别加密
    void encryptMessage(std::string inMessage , std::map<std::string , MessagePackageDagaType > &outMessage);

    void decryptMessage(std::string username , int deviceid , MessagePackageDagaType &inMessage , std::string &outMessage);

    const std::string &getTeamname() const;

    void setTeamname(const std::string &teamname);


    const std::string &getMyName() const;

    void setMyName(const std::string &myName);

    int getMyDeviceid() const;

    void setMyDeviceid(int myDeviceid);

    void initLocal(std::string myName , int myDeviceid , std::string teamName );

    virtual ~GroupSession();

    void setIk(KeyPairDataType &keyPairData);

    void setLocalId(int localId);

    void setLock(lockFunction  , lockFunction);
};


#endif //E2EEMESSAGE_GROUPSESSION_H
